package com.rrd.itext.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.itextpdf.text.Document;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;

public class ImpositionPdf{
    
    Rectangle pageSize;
    List<List<PdfImportedPage>> list;
    
    private ImpositionPdf(Rectangle pageSize){
        if(pageSize == null){
            this.pageSize = new Rectangle(1280, 400);
        }
        this.pageSize = pageSize;
    }
    
    public static ImpositionPdf getInstance(Rectangle pageSize){
        return new ImpositionPdf(pageSize);
    }

    public static ImpositionPdf getInstance(){
        return new ImpositionPdf(null);
    }
    
    public java.io.OutputStream impostione(String filePath)throws Exception{
        if(filePath == null){
            return null;
        }
        InputStream inputStream;
        if(filePath.startsWith("http://") || filePath.startsWith("https://")){
            return this.impostione(HttpClientUtil.getInstence().requestURL(filePath));
        }else{
            inputStream = new FileInputStream(new File(filePath));
        }
        byte[] remotePdf = IOUtil.toBytes(inputStream);
        return this.impostione(remotePdf);
    }
    
    public java.io.OutputStream impostione(byte[] remotePdf) throws Exception{
        Document document = new Document(pageSize, 0, 0, 0, 0);
        java.io.OutputStream out = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, out);
        writer.setCompressionLevel(0);
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        this.writePdf(writer, remotePdf);
        for(List<PdfImportedPage> l : list){
        	cb.addTemplate(l.get(0), 0, 0);
        	cb.addTemplate(l.get(1), this.pageSize.getWidth()/2, 0);
        	document.newPage();
        }
        document.close();
        return out;
    }
    
    private void writePdf(PdfWriter writer, byte[] remotePdf) throws IOException{
    	
    	PdfReader reader = new PdfReader(remotePdf);
    	PdfImportedPage importedStPage = null;
        PdfImportedPage importedEnPage = null;
        list = new ArrayList<List<PdfImportedPage>>();
        int n = reader.getNumberOfPages();
        int m = n/2;
        if(n%2 == 0){
        	for(int i=1;i<=m;i++){
            	List<PdfImportedPage> li = new ArrayList<PdfImportedPage>();
            	importedStPage = writer.getImportedPage(reader, i);
            	importedEnPage = writer.getImportedPage(reader, n-i+1);
            	li.add(importedStPage);
            	li.add(importedEnPage);
            	if(i%2 != 0){
            		Collections.reverse(li);
            	}
            	list.add(li);
            }
        }else{
        	for(int i=1;i<=m+1;i++){
            	List<PdfImportedPage> li = new ArrayList<PdfImportedPage>();
            	importedStPage = writer.getImportedPage(reader, i);
            	importedEnPage = writer.getImportedPage(reader, n-i+1);
            	if(i == m+1){
            		importedStPage = writer.getImportedPage(reader, m+1);
                	importedEnPage = null;
            	}
            	li.add(importedStPage);
            	li.add(importedEnPage);
            	if(i%2 != 0){
            		Collections.reverse(li);
            	}
            	list.add(li);
            }
        }
    	
    }
    
    
    
    public static void main(String[] args) throws Exception{
        ByteArrayOutputStream outs = (ByteArrayOutputStream)ImpositionPdf.getInstance(new Rectangle(1280,400)).impostione("D:/EnrollmentBookletPDFMarkedUpDec16th2011.pdf");
    	outs.writeTo(new FileOutputStream("D:/test.pdf"));
	}
    
}